"""
通用模型定义
===========
定义API响应、分页、错误处理等通用数据模型
"""

from typing import Any, Optional, List, Dict, Generic, TypeVar
from pydantic import Field
from .base_model import BaseModel


T = TypeVar('T')


class Response(BaseModel, Generic[T]):
    """标准API响应"""
    
    success: bool = Field(
        description="是否成功",
        example=True
    )
    
    message: str = Field(
        description="响应消息",
        example="操作成功"
    )
    
    data: Optional[T] = Field(
        default=None,
        description="响应数据"
    )
    
    timestamp: str = Field(
        description="响应时间戳",
        example="2023-09-26T10:30:00Z"
    )
    
    request_id: Optional[str] = Field(
        default=None,
        description="请求ID",
        example="req_123456789"
    )


class ErrorResponse(BaseModel):
    """错误响应"""
    
    success: bool = Field(
        default=False,
        description="是否成功"
    )
    
    error_code: str = Field(
        description="错误代码",
        example="INVALID_PARAMETER"
    )
    
    error_message: str = Field(
        description="错误消息",
        example="参数无效"
    )
    
    details: Optional[Dict[str, Any]] = Field(
        default=None,
        description="错误详情"
    )
    
    timestamp: str = Field(
        description="错误时间戳",
        example="2023-09-26T10:30:00Z"
    )
    
    request_id: Optional[str] = Field(
        default=None,
        description="请求ID"
    )


class ValidationError(BaseModel):
    """验证错误"""
    
    field: str = Field(
        description="字段名",
        example="email"
    )
    
    message: str = Field(
        description="错误消息",
        example="邮箱格式不正确"
    )
    
    code: str = Field(
        description="错误代码",
        example="INVALID_EMAIL"
    )


class ValidationErrorResponse(ErrorResponse):
    """验证错误响应"""
    
    validation_errors: List[ValidationError] = Field(
        description="验证错误列表",
        default_factory=list
    )


class PaginationMeta(BaseModel):
    """分页元数据"""
    
    page: int = Field(
        description="当前页码",
        ge=1,
        example=1
    )
    
    size: int = Field(
        description="每页大小",
        ge=1,
        le=1000,
        example=20
    )
    
    total: int = Field(
        description="总记录数",
        ge=0,
        example=100
    )
    
    pages: int = Field(
        description="总页数",
        ge=0,
        example=5
    )
    
    has_prev: bool = Field(
        description="是否有上一页",
        example=False
    )
    
    has_next: bool = Field(
        description="是否有下一页",
        example=True
    )
    
    prev_page: Optional[int] = Field(
        default=None,
        description="上一页页码"
    )
    
    next_page: Optional[int] = Field(
        default=None,
        description="下一页页码"
    )


class PaginatedResponse(BaseModel, Generic[T]):
    """分页响应"""
    
    success: bool = Field(
        default=True,
        description="是否成功"
    )
    
    message: str = Field(
        default="查询成功",
        description="响应消息"
    )
    
    data: List[T] = Field(
        description="数据列表",
        default_factory=list
    )
    
    meta: PaginationMeta = Field(
        description="分页元数据"
    )
    
    timestamp: str = Field(
        description="响应时间戳"
    )
    
    request_id: Optional[str] = Field(
        default=None,
        description="请求ID"
    )


class HealthCheck(BaseModel):
    """健康检查响应"""
    
    service: str = Field(
        description="服务名称",
        example="user-service"
    )
    
    status: str = Field(
        description="服务状态",
        example="healthy"
    )
    
    version: str = Field(
        description="服务版本",
        example="1.0.0"
    )
    
    timestamp: str = Field(
        description="检查时间戳"
    )
    
    dependencies: Dict[str, str] = Field(
        description="依赖服务状态",
        default_factory=dict,
        example={"database": "healthy", "cache": "healthy"}
    )
    
    uptime: Optional[str] = Field(
        default=None,
        description="运行时间",
        example="2d 3h 45m"
    )


class StatisticsData(BaseModel):
    """统计数据"""
    
    total_count: int = Field(
        description="总数",
        ge=0
    )
    
    active_count: int = Field(
        description="活跃数",
        ge=0
    )
    
    growth_rate: Optional[float] = Field(
        default=None,
        description="增长率（百分比）"
    )
    
    period: str = Field(
        description="统计周期",
        example="24h"
    )
    
    updated_at: str = Field(
        description="更新时间"
    )


class ConfigData(BaseModel):
    """配置数据"""
    
    key: str = Field(
        description="配置键",
        example="max_trade_amount"
    )
    
    value: Any = Field(
        description="配置值",
        example=10000
    )
    
    description: Optional[str] = Field(
        default=None,
        description="配置描述",
        example="最大交易金额"
    )
    
    is_public: bool = Field(
        default=False,
        description="是否公开"
    )
    
    category: Optional[str] = Field(
        default=None,
        description="配置分类",
        example="trading"
    )
